#ifndef ECHOSERVER_H_
#define ECHOSERVER_H_

#include "07_TcpServer.hpp"
#include "08_Threadpool.hpp"

namespace xy
{

// 声明一个外部变量来访问服务器实例
// 不过在封装Echo_Server以后，看样子就不用单独定义一个全局变量啦
// extern xy::TcpServer* g_server;

class MessageHandler;
//class DefaultMessageHandler;

class Echo_Server{
public:
    Echo_Server(unsigned short, const std::string&, int, int);

    void start();
    void stop();
    bool get_is_running(){ return _is_running; }

    // 定义回调函数(处理信息)
    static void signal_handler(int signum);     // 处理ctrl + c信号，优雅退出
    void on_connection(const TCPconnection::TCPconnectionPtr& conn);
    void on_message(const TCPconnection::TCPconnectionPtr& conn, const std::string &msg);
    void on_close(const TCPconnection::TCPconnectionPtr& conn);

    // 定义回调函数（用户业务逻辑）
    std::string handle_message1(const std::string& msg);
    std::string handle_message2(const std::string& msg);

    void set_message_handler1(std::shared_ptr<MessageHandler> handler)
    {
        _message_handler1 = handler;
    }
    void set_message_handler2(std::shared_ptr<MessageHandler> handler)
    {
        _message_handler2 = handler;
    }

private:
    TcpServer           _tcpserver;
    ThreadPool          _thread_pool;
    
    static Echo_Server* _instance;  // 信号处理函数通常需要全局函数或者静态成员函数
    std::shared_ptr<MessageHandler> _message_handler1;
    std::shared_ptr<MessageHandler> _message_handler2;
    bool                _is_running;
};


}

#endif
